import java.util.ResourceBundle;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Date;

import javax.imageio.ImageIO;

public class Thumbnails
{
    public static void main (String args[])
    {
		ResourceBundle rb = ResourceBundle.getBundle("ImageUpdate");
		String imgStrPath = rb.getString("imgupdate.images");
		String thbStrPath = rb.getString("imgupdate.thumbs");

        File imgFilePath = new File(imgStrPath);

        String images[] = imgFilePath.list();

        content_block:
        for(int i=0; i<images.length; i++)
        {

            if(!images[i].endsWith(".jpg") && !images[i].endsWith(".JPG") ) 
                continue content_block;
            
            String imgStr=imgStrPath+File.separator+images[i];
            String thbStr=thbStrPath+File.separator+images[i].toUpperCase();

            File imgFile = new File(imgStr);
            File thbFile = new File(thbStr); 
            
            Date thbDate = new Date(thbFile.lastModified());
            Date imgDate = new Date(imgFile.lastModified());

            if( ( imgFile.exists() && !thbFile.exists() )
              || ( imgFile.exists() && thbFile.exists() &&
               thbFile.lastModified() < imgFile.lastModified() ) )
            {
                System.out.println("Thumbnail Last Modified: " + thbDate);
                System.out.println("Image Last Modified: " + imgDate);
                System.out.println("Creating Thumbnail " + images[i].toUpperCase());

                try
                {
                    double maxSize=100.0;
                    double scale;            
                    int thumbWidth, thumbHeight;
    
                    BufferedImage bufi = ImageIO.read(imgFile);
                    BufferedImage thumbnailImage = null;
                
                    if(bufi.getWidth() > bufi.getHeight())
                    {
                        scale=maxSize/bufi.getWidth();
                        thumbWidth=(int)maxSize;
                        thumbHeight=(int)(maxSize * bufi.getHeight() / bufi.getWidth());
                    }
                    else
                    {
                        scale=maxSize/bufi.getHeight();
                        thumbWidth=(int)(maxSize * bufi.getWidth() / bufi.getHeight());
                        thumbHeight=(int)maxSize;
                    }
    
                    AffineTransform xform = AffineTransform.getScaleInstance(scale, scale);
                    AffineTransformOp op = new AffineTransformOp(xform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);            
    
                    thumbnailImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
                    Graphics2D g = thumbnailImage.createGraphics();
                    g.drawImage(bufi, op, 0, 0);
    
                    ImageIO.write(thumbnailImage, "jpeg", thbFile); 
                }
                catch(IOException ioe)
                {
                    System.out.println("I/O Exception");
                } // catch

            } // if !thbFile exists or thbFile is newer than imgFile
            // else System.out.println("Thumbnail for " + images[i] + " is up to date.");

        } // for i

    } // main

} // JpegTest